package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.makerspace.dao.CustomizeBulletinDAO;
import com.example.makerspace.dao.MessageDAO;
import com.example.makerspace.dao.UserFormsDAO;
import com.example.makerspace.domain.CustomizeBulletinDO;
import com.example.makerspace.domain.MessageDO;
import com.example.makerspace.domain.UserFormsDO;
import com.example.makerspace.dto.CustomizeBulletinDTO;
import com.example.makerspace.dto.PageRequest;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.dto.Result;
import com.example.makerspace.service.BulletinService;
import com.example.makerspace.utils.FileUtils;
import com.example.makerspace.utils.JwtUtils;
import com.example.makerspace.utils.ValidateCodeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BulletinServiceImpI implements BulletinService {
    private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";
   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";

    private final static String BULLETIN_PICTURE_PATH_PREFIX = "bulletin/";

    @Resource
    private CustomizeBulletinDAO customizeBulletinDAO;


    @Resource
    private MessageDAO messageDAO;

    @Resource
    private UserFormsDAO userFormsDAO;
    @Override
    public Result getBulletin(HttpServletRequest request) {
        String username = JwtUtils.getUsername(request);
        List<CustomizeBulletinDO> customizeBulletinDOS = customizeBulletinDAO.selectAllAndShow();
        List<CustomizeBulletinDO> customizeBulletinDOS1 = customizeBulletinDOS.stream().filter(item -> item.getType().equals(2)).collect(Collectors.toList());
        List<UserFormsDO> userFormsDOS = userFormsDAO.selectByUserName(username);
        List<UserFormsDO> collect = userFormsDOS.stream().filter(item -> item.getIsFill().equals(0)).collect(Collectors.toList());


        if(!CollectionUtils.isEmpty(collect)){
            String tableUrl = collect.get(0).getTableUrl();
            CustomizeBulletinDO customizeBulletinDO = customizeBulletinDOS1.get(0);
            customizeBulletinDO.setContent(tableUrl);
            int index = customizeBulletinDOS.indexOf(customizeBulletinDO);
            customizeBulletinDOS.set(index,customizeBulletinDO);
            return Result.success(customizeBulletinDOS);
        }
        return Result.success(customizeBulletinDOS);
    }

    @Override
    public Result addBulletin(CustomizeBulletinDTO customizeBulletinDTO) {
        CustomizeBulletinDO customizeBulletinDO = new CustomizeBulletinDO();
        BeanUtils.copyProperties(customizeBulletinDTO,customizeBulletinDO);
        String imageUrl = ValidateCodeUtils.generateCode(4);
        if (!ObjectUtils.isEmpty(customizeBulletinDTO.getImageUrl())) {
            String imagePath = FileUtils.saveMultipartFile2(customizeBulletinDTO.getImageUrl(), devHttpPrefix + BULLETIN_PICTURE_PATH_PREFIX + imageUrl,
                    FileUtils.staticPath + BULLETIN_PICTURE_PATH_PREFIX + imageUrl);
            customizeBulletinDO.setImageUrl(imagePath);
        }
        customizeBulletinDAO.insert(customizeBulletinDO);
        return Result.success("增加公告成功");
    }

    @Override
    public Result updateBulletin(CustomizeBulletinDTO customizeBulletinDTO) {
        CustomizeBulletinDO customizeBulletinDO = new CustomizeBulletinDO();
        BeanUtils.copyProperties(customizeBulletinDTO,customizeBulletinDO);
        String imageUrl = ValidateCodeUtils.generateCode(4);
        if (!ObjectUtils.isEmpty(customizeBulletinDTO.getImageUrl())) {
            String imagePath = FileUtils.saveMultipartFile2(customizeBulletinDTO.getImageUrl(), devHttpPrefix + BULLETIN_PICTURE_PATH_PREFIX + imageUrl,
                    FileUtils.staticPath + BULLETIN_PICTURE_PATH_PREFIX + imageUrl);
            customizeBulletinDO.setImageUrl(imagePath);
        }
        customizeBulletinDAO.updateById(customizeBulletinDO);
        return Result.success("修改公告成功");
    }

    @Override
    public Result deleteBulletin(Integer id) {
        customizeBulletinDAO.deleteById(id);
        return Result.success("删除公告成功");
    }

    @Override
    public Result listBulletin(PageRequest pageRequest) {
        IPage<CustomizeBulletinDO> customizeBulletinDOIPage;
        Pagination pagination = pageRequest.getPagination();
        String name = MapUtils.getString(pageRequest.getParams(), "name");
        if(name.equals("")){
            customizeBulletinDOIPage = customizeBulletinDAO.selectByPage(pagination);
            return  Result.success(customizeBulletinDOIPage);
        }else {
            customizeBulletinDOIPage =customizeBulletinDAO.selectByPageAndName(pagination,name);
            return  Result.success(customizeBulletinDOIPage);
        }
    }

    @Override
    public Result getLeftBulletin(PageRequest pageRequest, HttpServletRequest request) {
        String username = JwtUtils.getUsername(request);
        IPage<MessageDO> messageDOIPage = messageDAO.selectByUserNameAndAdmin(pageRequest.getPagination(), username);
        return Result.success(messageDOIPage);
    }
}
